package com.fudan.adse.service.impl;

import com.fudan.adse.component.GrpcServiceStub;
import com.fudan.adse.dao.FilmDao;
import com.fudan.adse.domain.cont.FilmStatus;
import com.fudan.adse.domain.cont.ReleaseType;
import com.fudan.adse.domain.dto.FilmDTO;
import com.fudan.adse.domain.po.*;
import com.fudan.adse.exception.ApiException;
import com.fudan.adse.mbg.mapper.FilmMapper;
import com.fudan.adse.service.FilmService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;

import static com.fudan.adse.api.ResultCode.FAILED;


/**
 * @author zhl
 */
@Slf4j
@Service
public class FilmServiceImpl implements FilmService {
    @Autowired
    private GrpcServiceStub grpcServiceStub;

    private FilmMapper filmMapper;
    private FilmDao filmDao;

    @Override
    public Film addFilm(FilmDTO filmDTO) {
        log.info("开始添加电影");
        Film film = new Film();
        BeanUtils.copyProperties(filmDTO, film);
        checkAndUpdateFilm(film);
        if (filmMapper.insert(film) > 0) {
            log.info("新电影 {} 添加成功！", film.getTitle());
            return film;
        } else {
            log.info("新电影 {} 添加失败！", film.getTitle());
            throw new ApiException(FAILED);
        }
    }

    @Override
    public void updateFilm(FilmDTO filmDTO) {
        Film film = filmMapper.selectByPrimaryKey(filmDTO.getId());
        if (film == null) {
            throw new ApiException("影片不存在");
        }
        film.setTitle(filmDTO.getTitle());
        film.setType(filmDTO.getType());
        film.setDescription(filmDTO.getDescription());
        film.setDirection(filmDTO.getDirection());
        film.setReleaseDate(filmDTO.getReleaseDate());
        film.setReleaseArea(filmDTO.getReleaseArea());
        film.setReleaseType(filmDTO.getReleaseType());
        film.setFootage(filmDTO.getFootage());
        film.setRate(filmDTO.getRate());
        film.setRateCount(filmDTO.getRateCount());
        checkAndUpdateFilm(film);
        if (filmMapper.updateByPrimaryKeySelective(film) <= 0) {
            throw new ApiException("更新电影信息失败");
        }
    }

    private void checkAndUpdateFilm(Film film) {
        if (film.getReleaseType() == null || film.getReleaseType().isEmpty()) {
            film.setReleaseType(ReleaseType.RELEASED.getType());
        }
        if (film.getReleaseArea() == null || film.getReleaseArea().isEmpty()) {
            film.setReleaseArea("中国大陆");
        }
        if (film.getStatus() == null || film.getStatus().isEmpty()) {
            film.setStatus(film.getReleaseDate().compareTo(new Date()) <= 0 ? FilmStatus.RELEASED.getStatus() :
                    FilmStatus.UNRELEASED.getStatus());
        }
        if (film.getRate() == null) {
            film.setRate(0f);
        }
        if (film.getRateCount() == null) {
            film.setRateCount(0);
        }
        if (Arrays.stream(ReleaseType.values()).noneMatch(t -> t.getType().equals(film.getReleaseType()))) {
            throw new ApiException("上映类型不存在");
        }
        if (Arrays.stream(FilmStatus.values()).noneMatch(t -> t.getStatus().equals(film.getStatus()))) {
            throw new ApiException("上映状态不存在");
        }
    }

    @Override
    public void deleteFilm(Integer filmId) {
        Film film = filmMapper.selectByPrimaryKey(filmId);
        if (film == null) {
            throw new ApiException("影片不存在");
        }
        // 下架
        film.setStatus(FilmStatus.DISABLE.getStatus());
        if (filmMapper.updateByPrimaryKeySelective(film) <= 0) {
            throw new ApiException("删除电影失败");
        }
    }

    @Override
    public void updateFilmStatus(Integer filmId, String status) {
        Film film = filmMapper.selectByPrimaryKey(filmId);
        if (film == null) {
            throw new ApiException("影片不存在");
        }
        film.setStatus(status);
        if (filmMapper.updateByPrimaryKeySelective(film) <= 0) {
            throw new ApiException(FAILED);
        }
    }

    @Autowired
    public void setFilmDao(FilmDao filmDao) {
        this.filmDao = filmDao;
    }

    @Autowired
    public void setFilmMapper(FilmMapper filmMapper) {
        this.filmMapper = filmMapper;
    }

}
